﻿using System;

namespace TomShane.Neoforce.Controls
{
    public class SettingsWindow : Window
    {
        private ComboBox mCmbResolution;
        private CheckBox mCbFullscreen;

        private string[] SupportedResolutions = { "640x480", "800x600", "1024x768", "1024x576", "1280x720", "1920x1080" };

        public SettingsWindow(Manager manager)
            : base(manager)
        {
        }

        public override void Init()
        {
            base.Init();

            InitControls();
        }

        private void InitControls()
        {
            mCmbResolution = (ComboBox)GetControl("cmbResolution");
            mCmbResolution.Init();
            foreach (string res in SupportedResolutions)
            {
                mCmbResolution.Items.Add(res);
            }
            mCmbResolution.ItemIndex = 0;

            mCbFullscreen = (CheckBox)GetControl("cbFullscreen");
            mCbFullscreen.Init();

            Button btnClose = (Button)GetControl("btnClose");
            btnClose.Click += new EventHandler(btnClose_Click);

            Button btnApply = (Button)GetControl("btnApply");
            btnApply.Click += new EventHandler(btnApply_Click);
        }

        void btnClose_Click(object sender, EventArgs e)
        {
            Close();
        }

        void btnApply_Click(object sender, EventArgs e)
        {
            string selectedRes = mCmbResolution.Text;
            var parts = selectedRes.Split('x');
            int w = Convert.ToInt32(parts[0]);
            int h = Convert.ToInt32(parts[1]);
            Manager.Graphics.PreferredBackBufferWidth = w;
            Manager.Graphics.PreferredBackBufferHeight = h;
            Manager.Graphics.IsFullScreen = mCbFullscreen.Checked;
            Manager.Graphics.ApplyChanges();
        }
    }
}
